home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / gfx / show / gs_src_gs.lha / gs5.03 / gdevxxf.c < prev    next >
C/C++ Source or Header  |  1995-11-15  |  14KB  |  466 lines

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevxxf.c */
  20. /* External font (xfont) implementation for X11. */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "x_.h"
  24. #include "gx.h"
  25. #include "gxdevice.h"
  26. #include "gdevx.h"
  27. #include "gsstruct.h"
  28. #include "gsutil.h"
  29. #include "gserrors.h"
  30. #include "gxxfont.h"
  31.  
  32. /* Define the smallest point size that we trust X to render reasonably well. */
  33. #define min_X_font_size 6
  34. /* Define the largest point size where X will do a better job than we can. */
  35. #define max_X_font_size 35
  36.  
  37. extern gx_device_X gs_x11_device;
  38.  
  39. extern const byte gs_map_std_to_iso[256];
  40. extern const byte gs_map_iso_to_std[256];
  41.  
  42. /* Declare the xfont procedures */
  43. private xfont_proc_lookup_font(x_lookup_font);
  44. private xfont_proc_char_xglyph(x_char_xglyph);
  45. private xfont_proc_char_metrics(x_char_metrics);
  46. private xfont_proc_render_char(x_render_char);
  47. private xfont_proc_release(x_release);
  48. private gx_xfont_procs x_xfont_procs = {
  49.     x_lookup_font,
  50.     x_char_xglyph,
  51.     x_char_metrics,
  52.     x_render_char,
  53.     x_release
  54. };
  55.  
  56. /* Return the xfont procedure record. */
  57. gx_xfont_procs *
  58. x_get_xfont_procs(gx_device *dev)
  59. {
  60.     return &x_xfont_procs;
  61. }
  62.  
  63. /* Define a X11 xfont. */
  64. typedef struct x_xfont_s x_xfont;
  65. struct x_xfont_s {
  66.       gx_xfont_common common;
  67.       gx_device_X *xdev;
  68.       XFontStruct *font;
  69.       int encoding_index;
  70.       int My;
  71.       int angle;
  72. };
  73. gs_private_st_dev_ptrs1(st_x_xfont, x_xfont, "x_xfont",
  74.   x_xfont_enum_ptrs, x_xfont_reloc_ptrs, xdev);
  75.  
  76. /* Look up a font. */
  77. private gx_xfont *
  78. x_lookup_font(gx_device *dev, const byte *fname, uint len,
  79.           int encoding_index, const gs_uid *puid, const gs_matrix *pmat,
  80.           gs_memory_t *mem)
  81. {
  82.     gx_device_X *xdev = (gx_device_X *) dev;
  83.     x_xfont *xxf;
  84.     char x11template[256];
  85.     char *x11fontname = NULL;
  86.     XFontStruct *x11font;
  87.     x11fontmap *fmp;
  88.     int i;
  89.     double height;
  90.     int size;
  91.     int xwidth, xheight, angle;
  92.     Boolean My;
  93.     Boolean scalable_font = False;
  94.  
  95.     if (!xdev->useXFonts) return NULL;
  96.  
  97.     if (pmat->xy == 0 && pmat->yx == 0) {
  98.     xwidth  = fabs(pmat->xx * 1000) + 0.5;
  99.     xheight = fabs(pmat->yy * 1000) + 0.5;
  100.     height  = fabs(pmat->yy * 1000);
  101.     if (pmat->xx > 0) angle = 0;
  102.     else angle = 180;
  103.     My = (pmat->xx > 0 && pmat->yy > 0) || (pmat->xx < 0 && pmat->yy < 0);
  104.     } else if (pmat->xx == 0 && pmat->yy == 0) {
  105.     xwidth  = fabs(pmat->xy * 1000) + 0.5;
  106.     xheight = fabs(pmat->yx * 1000) + 0.5;
  107.     height  = fabs(pmat->yx * 1000);
  108.     if (pmat->yx < 0) angle = 90;
  109.     else angle = 270;
  110.     My = (pmat->yx > 0 && pmat->xy < 0) || (pmat->yx < 0 && pmat->xy > 0);
  111.     } else {
  112.     return NULL;
  113.     }
  114.  
  115.     /* Don't do very small fonts, where font metrics are way off */
  116.     /* due to rounding and the server does a very bad job of scaling, */
  117.     /* or very large fonts, where we can do just as good a job and */
  118.     /* the server may lock up the entire window system while rasterizing */
  119.     /* the whole font. */
  120.     if ( xwidth < min_X_font_size || xwidth > max_X_font_size ||
  121.      xheight < min_X_font_size || xheight > max_X_font_size
  122.        )
  123.       return NULL;
  124.  
  125.     if (!xdev->useFontExtensions && (My || angle != 0)) return NULL;
  126.  
  127.     if (encoding_index == 0 || encoding_index == 1) {
  128.     int tried_other_encoding = 0;
  129.  
  130.     fmp = xdev->regular_fonts;
  131.     while (fmp) {
  132.         if (len == strlen(fmp->ps_name) &&
  133.         strncmp(fmp->ps_name, (const char *)fname, len) == 0) break;
  134.         fmp = fmp->next;
  135.     }
  136.     if (fmp == NULL) return NULL;
  137.     while (True) {
  138.         if (encoding_index == 0) {
  139.         if (fmp->std_count == -1) {
  140.             sprintf(x11template, "%s%s", fmp->x11_name,
  141.                 "-*-*-*-*-*-*-Adobe-fontspecific");
  142.             fmp->std_names = XListFonts(xdev->dpy, x11template, 32,
  143.                         &fmp->std_count);
  144.         }
  145.         if (fmp->std_count) {
  146.             for (i = 0; i < fmp->std_count; i++) {
  147.             char *szp = fmp->std_names[i] + strlen(fmp->x11_name)+1;
  148.  
  149.             size = 0;
  150.             while (*szp >= '0' && *szp <= '9')
  151.                 size = size * 10 + *szp++ - '0';
  152.             if (size == 0) {
  153.                 scalable_font = True;
  154.                 continue;
  155.             }
  156.             if (size == xheight) {
  157.                 x11fontname = fmp->std_names[i];
  158.                 break;
  159.             }
  160.             }
  161.             if (!x11fontname && scalable_font &&
  162.             xdev->useScalableFonts) {
  163.             sprintf(x11template, "%s-%d%s", fmp->x11_name,
  164.                 xheight, "-0-0-0-*-0-Adobe-fontspecific");
  165.             x11fontname = x11template;
  166.             }
  167.             if (x11fontname) break;
  168.         }
  169.         if (tried_other_encoding) return NULL;
  170.         encoding_index = 1;
  171.         tried_other_encoding = 1;
  172.         } else if (encoding_index == 1) {
  173.         if (fmp->iso_count == -1) {
  174.             sprintf(x11template, "%s%s", fmp->x11_name,
  175.                 "-*-*-*-*-*-*-ISO8859-1");
  176.             fmp->iso_names = XListFonts(xdev->dpy, x11template, 32,
  177.                         &fmp->iso_count);
  178.         }
  179.         if (fmp->iso_count) {
  180.             for (i = 0; i < fmp->iso_count; i++) {
  181.             char *szp = fmp->iso_names[i] + strlen(fmp->x11_name)+1;
  182.  
  183.             size = 0;
  184.             while (*szp >= '0' && *szp <= '9')
  185.                 size = size * 10 + *szp++ - '0';
  186.             if (size == 0) {
  187.                 scalable_font = True;
  188.                 continue;
  189.             }
  190.             if (size == xheight) {
  191.                 x11fontname = fmp->iso_names[i];
  192.                 break;
  193.             }
  194.             }
  195.             if (!x11fontname && scalable_font &&
  196.             xdev->useScalableFonts) {
  197.             sprintf(x11template, "%s-%d%s", fmp->x11_name,
  198.                 xheight, "-0-0-0-*-0-ISO8859-1");
  199.             x11fontname = x11template;
  200.             }
  201.             if (x11fontname) break;
  202.         }
  203.         if (tried_other_encoding) return NULL;
  204.         encoding_index = 0;
  205.         tried_other_encoding = 1;
  206.         }
  207.     }
  208.     } else if (encoding_index == 2 || encoding_index == 3) {
  209.     if (encoding_index == 2) fmp = xdev->symbol_fonts;
  210.     if (encoding_index == 3) fmp = xdev->dingbat_fonts;
  211.     while (fmp) {
  212.         if (len == strlen(fmp->ps_name) &&
  213.         strncmp(fmp->ps_name, (const char *)fname, len) == 0)
  214.         break;
  215.         fmp = fmp->next;
  216.     }
  217.     if (fmp == NULL) return NULL;
  218.     if (fmp->std_count == -1) {
  219.         sprintf(x11template, "%s%s", fmp->x11_name,
  220.             "-*-*-*-*-*-*-Adobe-fontspecific");
  221.         fmp->std_names = XListFonts(xdev->dpy, x11template, 32,
  222.                     &fmp->std_count);
  223.     }
  224.     if (fmp->std_count) {
  225.         for (i = 0; i < fmp->std_count; i++) {
  226.         char *szp = fmp->std_names[i] + strlen(fmp->x11_name)+1;
  227.  
  228.         size = 0;
  229.         while (*szp >= '0' && *szp <= '9')
  230.             size = size * 10 + *szp++ - '0';
  231.         if (size == 0) {
  232.             scalable_font = True;
  233.             continue;
  234.         }
  235.         if (size == xheight) {
  236.             x11fontname = fmp->std_names[i];
  237.             break;
  238.         }
  239.         }
  240.         if (!x11fontname && scalable_font && xdev->useScalableFonts) {
  241.         sprintf(x11template, "%s-%d%s", fmp->x11_name, xheight,
  242.             "-0-0-0-*-0-Adobe-fontspecific");
  243.         x11fontname = x11template;
  244.         }
  245.         if (!x11fontname) return NULL;
  246.     } else {
  247.         return NULL;
  248.     }
  249.     } else {
  250.     return NULL;
  251.     }
  252.  
  253.     if (xwidth != xheight || angle != 0 || My) {
  254.     if (!xdev->useScalableFonts || !scalable_font) return NULL;
  255.     sprintf(x11template, "%s%s+%d-%d+%d%s",
  256.         fmp->x11_name, My ? "+My" : "",
  257.         angle*64 , xheight, xwidth,
  258.         encoding_index == 1 ? "-0-0-0-*-0-ISO8859-1" :
  259.         "-0-0-0-*-0-Adobe-fontspecific");
  260.     x11fontname = x11template;
  261.     }
  262.  
  263.     x11font = XLoadQueryFont(xdev->dpy, x11fontname);
  264.     if (x11font == NULL)
  265.     return NULL;
  266.     /* Don't bother with 16-bit or 2 byte fonts yet */
  267.     if (x11font->min_byte1 || x11font->max_byte1) {
  268.     XFreeFont(xdev->dpy, x11font);
  269.     return NULL;
  270.     }
  271.     xxf = gs_alloc_struct(mem, x_xfont, &st_x_xfont, "x_lookup_font");
  272.     if (xxf == NULL)
  273.     return NULL;
  274.     xxf->common.procs = &x_xfont_procs;
  275.     xxf->xdev = xdev;
  276.     xxf->font = x11font;
  277.     xxf->encoding_index = encoding_index;
  278.     xxf->My = My ? -1 : 1;
  279.     xxf->angle = angle;
  280.     if (xdev->logXFonts) {
  281.     fprintf(stdout, "Using %s\n", x11fontname);
  282.     fprintf(stdout, "  for %s at %g pixels.\n", fmp->ps_name, height);
  283.     fflush(stdout);
  284.     }
  285.     return (gx_xfont *) xxf;
  286. }
  287.  
  288. /* Convert a character name or index to an xglyph code. */
  289. private gx_xglyph
  290. x_char_xglyph(gx_xfont *xf, gs_char chr, int encoding_index,
  291.           gs_glyph glyph, gs_proc_glyph_name_t glyph_name_proc)
  292. {
  293.     x_xfont *xxf = (x_xfont *) xf;
  294.  
  295.     if (chr == gs_no_char)
  296.     return gx_no_xglyph;    /* can't look up names yet */
  297.     if (encoding_index != xxf->encoding_index) {
  298.     if (encoding_index == 0 && xxf->encoding_index == 1)
  299.         chr = gs_map_std_to_iso[chr];
  300.     else if (encoding_index == 1 && xxf->encoding_index == 0)
  301.         chr = gs_map_iso_to_std[chr];
  302.     else
  303.         return gx_no_xglyph;
  304.     if (chr == 0) return gx_no_xglyph;
  305.     }
  306.     if (chr < xxf->font->min_char_or_byte2 ||
  307.     chr > xxf->font->max_char_or_byte2)
  308.     return gx_no_xglyph;
  309.     if (xxf->font->per_char) {
  310.     int i = chr - xxf->font->min_char_or_byte2;
  311.     XCharStruct *xc = &(xxf->font->per_char[i]);
  312.  
  313.     if ((xc->lbearing == 0) && (xc->rbearing == 0) &&
  314.         (xc->ascent == 0) && (xc->descent == 0))
  315.         return gx_no_xglyph;
  316.     }
  317.     return (gx_xglyph) chr;
  318. }
  319.  
  320. /* Get the metrics for a character. */
  321. private int
  322. x_char_metrics(gx_xfont *xf, gx_xglyph xg, int wmode,
  323.            gs_point *pwidth, gs_int_rect *pbbox)
  324. {
  325.     x_xfont *xxf = (x_xfont *) xf;
  326.  
  327.     if (wmode != 0)
  328.     return gs_error_undefined;
  329.     if (xxf->font->per_char == NULL) {
  330.     if (xxf->angle == 0) {
  331.         pwidth->x = xxf->font->max_bounds.width;
  332.         pwidth->y = 0;
  333.     } else if (xxf->angle == 90) {
  334.         pwidth->x = 0;
  335.         pwidth->y = -xxf->My * xxf->font->max_bounds.width;
  336.     } else if (xxf->angle == 180) {
  337.         pwidth->x = -xxf->font->max_bounds.width;
  338.         pwidth->y = 0;
  339.     } else if (xxf->angle == 270) {
  340.         pwidth->x = 0;
  341.         pwidth->y = xxf->My * xxf->font->max_bounds.width;
  342.     }
  343.     pbbox->p.x = xxf->font->max_bounds.lbearing;
  344.     pbbox->q.x = xxf->font->max_bounds.rbearing;
  345.     pbbox->p.y = -xxf->font->max_bounds.ascent;
  346.     pbbox->q.y = xxf->font->max_bounds.descent;
  347.     } else {
  348.     int i = xg - xxf->font->min_char_or_byte2;
  349.  
  350.     if (xxf->angle == 0) {
  351.         pwidth->x = xxf->font->per_char[i].width;
  352.         pwidth->y = 0;
  353.     } else if (xxf->angle == 90) {
  354.         pwidth->x = 0;
  355.         pwidth->y = -xxf->My * xxf->font->per_char[i].width;
  356.     } else if (xxf->angle == 180) {
  357.         pwidth->x = -xxf->font->per_char[i].width;
  358.         pwidth->y = 0;
  359.     } else if (xxf->angle == 270) {
  360.         pwidth->x = 0;
  361.         pwidth->y = xxf->My * xxf->font->per_char[i].width;
  362.     }
  363.     pbbox->p.x = xxf->font->per_char[i].lbearing;
  364.     pbbox->q.x = xxf->font->per_char[i].rbearing;
  365.     pbbox->p.y = -xxf->font->per_char[i].ascent;
  366.     pbbox->q.y = xxf->font->per_char[i].descent;
  367.     }
  368.     return 0;
  369. }
  370.  
  371. /* Render a character. */
  372. private int
  373. x_render_char(gx_xfont *xf, gx_xglyph xg, gx_device *dev,
  374.           int xo, int yo, gx_color_index color, int required)
  375. {
  376.     x_xfont *xxf = (x_xfont *) xf;
  377.     gx_device_X *xdev = xxf->xdev;
  378.     char chr = (char)xg;
  379.     gs_point wxy;
  380.     gs_int_rect bbox;
  381.     int x, y, w, h;
  382.     int code;
  383.  
  384.     if (dev->dname == gs_x11_device.dname) {
  385.     code = (*xf->common.procs->char_metrics) (xf, xg, 0, &wxy, &bbox);
  386.     if (code < 0) return code;
  387.     set_fill_style(FillSolid);
  388.     set_fore_color(color);
  389.     set_function(GXcopy);
  390.     set_font(xxf->font->fid);
  391.     XDrawString(xdev->dpy, xdev->dest, xdev->gc, xo, yo, &chr, 1);
  392.     if (xdev->bpixmap != (Pixmap) 0) {
  393.         x = xo + bbox.p.x;
  394.         y = yo + bbox.p.y;
  395.         w = bbox.q.x - bbox.p.x;
  396.         h = bbox.q.y - bbox.p.y;
  397.         fit_fill(dev, x, y, w, h);
  398.         x_update_add(dev, x, y, w, h);
  399.     }
  400.     return 0;
  401.     } else if (!required)
  402.     return -1;    /* too hard */
  403.     else {
  404.     /* Display on an intermediate bitmap, then copy the bits. */
  405.     int wbm, raster;
  406.     int i;
  407.     XImage *xim;
  408.     Pixmap xpm;
  409.     GC fgc;
  410.     byte *bits;
  411.  
  412.     dev_proc_copy_mono((*copy_mono)) = dev_proc(dev, copy_mono);
  413.  
  414.     code = (*xf->common.procs->char_metrics) (xf, xg, 0, &wxy, &bbox);
  415.     if (code < 0) return code;
  416.     w = bbox.q.x - bbox.p.x;
  417.     h = bbox.q.y - bbox.p.y;
  418.     wbm = round_up(w, align_bitmap_mod * 8);
  419.     raster = wbm >> 3;
  420.     bits = (byte *) gs_malloc(h, raster, "x_render_char");
  421.     if (bits == 0) return gs_error_limitcheck;
  422.     xpm = XCreatePixmap(xdev->dpy, xdev->win, w, h, 1);
  423.     fgc = XCreateGC(xdev->dpy, xpm, None, NULL);
  424.     XSetForeground(xdev->dpy, fgc, 0);
  425.     XFillRectangle(xdev->dpy, xpm, fgc, 0, 0, w, h);
  426.     XSetForeground(xdev->dpy, fgc, 1);
  427.     XSetFont(xdev->dpy, fgc, xxf->font->fid);
  428.     XDrawString(xdev->dpy, xpm, fgc, -bbox.p.x, -bbox.p.y, &chr, 1);
  429.     xim = XGetImage(xdev->dpy, xpm, 0, 0, w, h, 1, ZPixmap);
  430.     i = 0;
  431.     for (y = 0; y < h; y++) {
  432.         char b = 0;
  433.  
  434.         for (x = 0; x < wbm; x++) {
  435.         b = b << 1;
  436.         if (x < w)
  437.             b += XGetPixel(xim, x, y);
  438.         if ((x & 7) == 7)
  439.             bits[i++] = b;
  440.         }
  441.     }
  442.     code = (*copy_mono) (dev, bits, 0, raster, gx_no_bitmap_id,
  443.                  xo + bbox.p.x, yo + bbox.p.y, w, h,
  444.                  gx_no_color_index, color);
  445.     gs_free((char *)bits, h, raster, "x_render_char");
  446.     XFreePixmap(xdev->dpy, xpm);
  447.     XFreeGC(xdev->dpy, fgc);
  448.     XDestroyImage(xim);
  449.     return (code < 0 ? code : 0);
  450.     }
  451. }
  452.  
  453. /* Release an xfont. */
  454. private int
  455. x_release(gx_xfont *xf, gs_memory_t *mem)
  456. {
  457. #if 0
  458.     /* The device may not be open.  Cannot reliably free the font. */
  459.     x_xfont *xxf = (x_xfont *) xf;
  460.     XFreeFont(xxf->xdev->dpy, xxf->font);
  461. #endif
  462.     if (mem != NULL)
  463.     gs_free_object(mem, xf, "x_release");
  464.     return 0;
  465. }
  466.